/*
 *  WilNoteData.cpp
 *  Wilson
 *
 *  Created by Sebastian Orfino on 8/19/11.
 *  Copyright 2011 __MyCompanyName__. All rights reserved.
 *
 */


#include "VCPlugInHeaders.h"

// API includes
#include "CPMUnknown.h"
//#include "SnapshotUtilsEx.h"
#include "IPMStream.h"
//#include "StreamUtil.h"

//#include "IPathGeometry.h"
//#include "SnapshotUtilsEx.h"
//#include "ISpread.h"
//#include "IPasteboardUtils.h"
//#include "IGeometryFacade.h"
//#include "IHierarchy.h"
//#include "IControlView.h"
//#include "ILayoutUtils.h"

// project includes
#include "WilID.h"
#include "IWilNoteData.h"
#include "boost/date_time/posix_time/posix_time.hpp" 
//#include "boost/shared_ptr.hpp"
//#include "Base64MemoryXferBytes.h"


class WilNoteData : public CPMUnknown<IWilNoteData> {

	
	/*WideString comentario_;
	WideString quien_;
	boost::posix_time::ptime cuando_;
	WideString miniImagenBase64;*/
	
	adobe::zuid_t uid_;
		
public:
	
	WilNoteData(IPMUnknown* ipu);
	virtual ~WilNoteData() { }

	/**
	 Crea y devuelve una imagen miniatura codificada en base64 del sector donde se encuentra el pageItem.
	 La imagen es en formato PNG.
	
	virtual std::string MiniImagenBase64() const; */
	
	/** Devuelve el id de la anotacion
	 */
	virtual adobe::zuid_t uid() const { return uid_; }

	virtual void touch();
	/**
	 Because this is a persistent interface, it must support the ReadWrite method. This method is used for 
	 both writing information out to the database and reading information in from the database.
	 
	 Note: the first thing we read/write here is a version number.  It is used to identify which plug-in
	 is used to write out this BPI data.  Since this is the first version of the BPI plug-in, thus the version
	 number is 1, and then followed by the BPI data.  Later if there is a newer version of this plug-in which
	 will write out an extra field of data, then we can just come to this routine, and check the version number
	 of the doc, and then read in the data accordingly.  this will make sure the newer plug-in is backward
	 compatible with an older version of document.  Lastly, since the version number should be the first thing
	 to read in for this interface, all the new data should be append at the bottom.
	 
	 @param stream contains the stream to be written or read.
	 @param implementation the implementation ID.
	 */
	void ReadWrite(IPMStream* stream, ImplementationID implementation);
	
private:
		//typedef boost::shared_ptr<SnapshotUtilsEx> snapshot_ptr;
	
};

CREATE_PERSIST_PMINTERFACE(WilNoteData,kWilNoteDataImpl);


void WilNoteData::touch() 
{
	PreDirty();
}

/*std::string  WilNoteData::MiniImagenBase64() const
{
	
do {
	
	UIDList itemList(this);
	
	//Calcular los limites del snapshot
	PMRect clippingBounds = Utils<Facade::IGeometryFacade>()->GetItemsBounds(itemList, Transform::SpreadCoordinates(), Geometry::OuterStrokeBounds());

	InterfacePtr<IHierarchy> iHierarchy(this,UseDefaultIID());
	InterfacePtr<IControlView> iControlView(this,UseDefaultIID());
	
	InterfacePtr<ISpread> dSpread(Utils<ILayoutUtils>()->QueryParentFor(this,IID_ISPREAD),IID_ISPREAD);
	
	InterfacePtr<ISpread> iSpread(Utils<IPasteboardUtils>()->QuerySpread(iHierarchy), UseDefaultIID());
	
	UID hSpread = Utils<ILayoutUtils>()->GetDrawingPageUID(iHierarchy);
	bool v = hSpread.IsValid();
	
	UIDRef yo(::GetUIDRef(this));
	/*const UIDRef& itemUIDRef, 
	 const PMReal& xScaleFactor = 1.0,
	 const PMReal& yScaleFactor = 1.0,
	 const PMReal& desiredRes = 72.0,
	 const PMReal& minRes = 72.0,
	 const PMReal& bleedAmount = 0.0,
	 SSColorSpaceFamily colorSpace = kCsRGB,
	 bool8 addTransparencyAlpha = kFalse,
	 bool8 simulateOverprint = kFalse*/
	//ahora genero la mini imagen de la nota.
	/*snapshot_ptr snapshot( new SnapshotUtilsEx(clippingBounds,
											   PMMatrix(),
											   ::GetUIDRef(iSpread),
											   1.0, //xScaleFactor
											   1.0, //yScaleFactor
											   72.0, //res
											   72.0, //min res
											   10.0, //bleed
											   SnapshotUtilsEx::kCsRGB,
											   kTrue //add transparency alpha
											   )
						  );
	/*snapshot_ptr snapshot(new SnapshotUtilsEx(::GetUIDRef(this),
											  1.0,
											  1.0,
											  72.0,
											  72.0,
											  10.0));*/
	
	/*if (!snapshot) break;
	
	// We have to call Draw on the snapshot
	if( snapshot->Draw(IShape::kPreviewMode | IShape::kPrinting) != kSuccess)
		break;

	Base64MemoryXferBytes buffer;
	InterfacePtr<IPMStream> base64Stream(StreamUtil::CreateMemoryStreamWrite(&buffer));
	
	if (!base64Stream) break;
	
	ErrorCode err = snapshot->ExportImageToPNG(base64Stream);
	if (err != kSuccess) break;
	
	return buffer.GetBuffer();		

	
} while(kFalse);
	
	return "";
}*/
	
//ctor
WilNoteData::WilNoteData(IPMUnknown* boss):CPMUnknown<IWilNoteData>(boss)//, cuando_(boost::posix_time::second_clock::universal_time())
{
	
}

void WilNoteData::ReadWrite(IPMStream* stream, ImplementationID implementation)
{
	using namespace boost::posix_time;
	const int16 VERSION = 0x02;
	int16 version  = VERSION;
	
	stream->XferInt16(version);
	
	if (version != VERSION)
		return;
	//comentario_.ReadWrite(stream);
	//quien_.ReadWrite(stream);
	/*if (stream->IsWriting()) {
		PMString cuando(to_iso_string(cuando_).c_str());
		cuando.ReadWrite(stream);
	}
	else {
		PMString cuando;
		cuando.ReadWrite(stream);
		cuando_ = ptime(from_iso_string(cuando.GetPlatformString()));
	}
	
	miniImagenBase64.ReadWrite(stream);
*/
	if (stream->IsWriting()) {
		PMString id(uid().c_str());
		id.ReadWrite(stream);
	}
	else {
		PMString idStr;
		idStr.ReadWrite(stream);
		uid_ = adobe::zuid_t(idStr.GetPlatformString());
	}
}




